package com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.productservice.controller;

import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.productservice.dto.ProductCleanFrozenStockDTO;
import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.productservice.dto.ProductFrozenStockDTO;
import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.productservice.dto.ProductRollbackFrozenStockDTO;
import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.productservice.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 商品 控制器
 *
 * @author ziyucoding
 * @date 2022-11-06
 **/
@RestController
@RequestMapping("/product")
public class ProductController {

    private Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    @GetMapping("/get-stock")
    public Map<String, Object> getStock(@RequestParam("productId") Long productId) {
        boolean isSuccess = true;
        Integer stock = 0;
        try {
            stock = productService.getStock(productId);
        } catch (Exception e) {
            logger.error("获取库存失败", e);
            isSuccess = false;
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "成功");

        Map<String, Object> data = new HashMap<>();
        data.put("isSuccess", isSuccess);
        data.put("stock", stock == null ? 0 : stock);
        result.put("data", data);
        return result;
    }

    @GetMapping("/get-frozen-stock")
    public Map<String, Object> getFrozenStock(@RequestParam("productId") Long productId) {
        boolean isSuccess = true;
        Integer stock = 0;
        try {
            stock = productService.getFrozenStock(productId);
        } catch (Exception e) {
            logger.error("获取冻结库存失败", e);
            isSuccess = false;
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "成功");

        Map<String, Object> data = new HashMap<>();
        data.put("isSuccess", isSuccess);
        data.put("stock", stock == null ? 0 : stock);
        result.put("data", data);
        return result;
    }

    @PostMapping("/frozen-stock")
    public Map<String, Object> frozenStock(@RequestBody ProductFrozenStockDTO dto) {
        logger.info("[frozenStock] 收到冻结库存请求, 商品:{}, 数量:{}", dto.getProductId(),
                dto.getAmount());
        boolean isSuccess = false;
        try {
            productService.frozenStock(dto.getProductId(), dto.getAmount());
            isSuccess = true;
        } catch (Exception e) {
            logger.error("冻结库存失败", e);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "成功");
        result.put("data", isSuccess);
        return result;
    }

    @PostMapping("/clean-frozen-stock")
    public Map<String, Object> cleanFrozenStock(@RequestBody ProductCleanFrozenStockDTO dto) {
        logger.info("[cleanFrozenStock] 收到释放冻结库存请求, 商品:{}, 数量:{}", dto.getProductId(),
                dto.getAmount());
        boolean isSuccess = false;
        try {
            productService.cleanFrozenStock(dto.getProductId(), dto.getAmount());
            // 正常扣除库存，返回 true
            isSuccess = true;
        } catch (Exception e) {
            logger.error("释放冻结库存失败", e);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "成功");
        result.put("data", isSuccess);
        return result;
    }

    @PostMapping("/rollback-frozen-stock")
    public Map<String, Object> rollbackFrozenStock(@RequestBody ProductRollbackFrozenStockDTO dto) {
        logger.info("[frozenStock] 收到回退冻结库存请求, 商品:{}, 数量:{}", dto.getProductId(),
                dto.getAmount());
        boolean isSuccess = false;
        try {
            productService.rollbackFrozenStock(dto.getProductId(), dto.getAmount());
            isSuccess = true;
        } catch (Exception e) {
            logger.error("回退冻结库存失败", e);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "成功");
        result.put("data", isSuccess);
        return result;
    }


}
